package com.coin900.coin.service.coin;

import com.coin900.coin.base.CoinConstants;
import com.coin900.coin.base.FuConstants;
import com.coin900.coin.persist.coin.*;
import com.value.mybatis.IDaoRouter;
import com.value.mybatis.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class CoinProjectService implements ICoinProjectService {

    @Override
    public List<CoinProjectVO> loadCoinProject(Map<String, Object> params) {
        return this.daoRouter.query("CoinProject.loadCoinProject", params);
    }

    @Override
    public List<CoinUserProjectVO> loadUserProject(Map<String, Object> params) {
        return this.daoRouter.query("CoinProject.loadUserProject", params);
    }

    @Override
    public List<CoinUserProjectVO> loadUserProjectForPage(Pagination pagination) {
        return this.daoRouter.query("CoinProject.loadUserProjectForPage", pagination);
    }

    @Override
    public List<CoinProjectVO> loadCoinProjectForPage(Pagination pagination) {
        return this.daoRouter.query("CoinProject.loadCoinProjectForPage", pagination);
    }

    @Override
    public void insertCoinProject(CoinProjectVO coinProjectVO) {
        // 项目新建的默认数据
        coinProjectVO.setFundedAmount(0L);
        coinProjectVO.setParticipantNumber(0L);
        coinProjectVO.setStatus(CoinConstants.COIN_PROJECT_STATUS_UNPUBLISHED);
        coinProjectVO.setCheckStatus(CoinConstants.COIN_CHECK_STATUS_ING);
        coinProjectVO.setPublishStatus(CoinConstants.COIN_PUBLISH_STATUS_UNPUBLICHED);
        this.daoRouter.insert("CoinProject.insertCoinProject", coinProjectVO);
        // 项目支持的虚拟币
        List<CoinProjectCoinVO> projectCoinList = coinProjectVO.getProjectCoinList();
        if (projectCoinList != null) {
            this.projectCoinService.insertCoinProjectCoinBatch(coinProjectVO.getId(), projectCoinList);
        }
    }

    @Override
    public void updateCoinProject(CoinProjectVO coinProjectVO) {
        this.daoRouter.update("CoinProject.updateCoinProject", coinProjectVO);
        List<CoinProjectCoinVO> projectCoinList = coinProjectVO.getProjectCoinList();
        if (projectCoinList != null) {
            // 更新项目支持的虚拟币
            // 删除全部
            CoinProjectCoinVO coinProjectCoinVO = new CoinProjectCoinVO();
            coinProjectCoinVO.setProjectId(coinProjectVO.getId());
            coinProjectCoinVO.setModifiedDate(coinProjectVO.getModifiedDate());
            coinProjectCoinVO.setModifiedBy(coinProjectCoinVO.getModifiedBy());
            this.projectCoinService.deleteCoinProjectCoin(coinProjectCoinVO);
            // 插入新的
            this.projectCoinService.insertCoinProjectCoinBatch(coinProjectVO.getId(), projectCoinList);
        }
        /***** 项目发布检查订单是否有预售期支付的订单，如果有将订单支付 *****/
        Integer publishStatus = coinProjectVO.getPublishStatus();
        if (CoinConstants.COIN_PROJECT_STATUS_ING == publishStatus) {
            Map<String, Object> params = new HashMap<>();
            params.put("projectId", coinProjectVO.getId());
            params.put("type", 2);
            params.put("orderStatus", CoinConstants.COIN_ORDER_STATUS_PAID);
            params.put("dataStatus", 1);
            List<CoinOrderVO> coinOrderVOS = this.orderService.loadCoinOrder(params);
            if (!coinOrderVOS.isEmpty()) {
                for (CoinOrderVO coinOrderVO : coinOrderVOS) {
                    this.orderService.doPayForTheOrderWhenProjectIsRelease(coinOrderVO);
                }
            }
        }
    }

    @Override
    public void doPayForTheOrder(CoinProjectVO coinProjectVO) {
        this.daoRouter.update("CoinProject.doPayForTheOrder", coinProjectVO);
    }

    @Override
    public void deleteCoinProject(CoinProjectVO coinProjectVO) {
        this.daoRouter.update("CoinProject.deleteCoinProject", coinProjectVO);
        // 删除项目虚拟币表
        CoinProjectCoinVO coinProjectCoinVO = new CoinProjectCoinVO();
        coinProjectCoinVO.setModifiedBy(coinProjectVO.getModifiedBy());
        coinProjectCoinVO.setModifiedDate(coinProjectVO.getModifiedDate());
        coinProjectCoinVO.setDataStatus(0);
        coinProjectCoinVO.setProjectId(coinProjectVO.getId());
        this.projectCoinService.deleteCoinProjectCoin(coinProjectCoinVO);
    }

    @Autowired
    private ICoinOrderService orderService;
    @Autowired
    private ICoinProjectCoinService projectCoinService;
    @Autowired
    private IDaoRouter daoRouter;

}